package com.xueqiu.lovepetmedicalmanagersystem.controller;

import com.xueqiu.lovepetmedicalmanagersystem.pojo.Owners;
import com.xueqiu.lovepetmedicalmanagersystem.pojo.PetTypes;
import com.xueqiu.lovepetmedicalmanagersystem.pojo.Pets;
import com.xueqiu.lovepetmedicalmanagersystem.pojo.Visits;
import com.xueqiu.lovepetmedicalmanagersystem.service.OwnersService;
import com.xueqiu.lovepetmedicalmanagersystem.service.PetService;
import com.xueqiu.lovepetmedicalmanagersystem.service.PetTypesService;
import com.xueqiu.lovepetmedicalmanagersystem.service.VisitsService;
import com.xueqiu.lovepetmedicalmanagersystem.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author xueqiu
 */
@Controller
@RequestMapping("/pet")
public class PetController {

    @Autowired
    private PetService petService;
    @Autowired
    private OwnersService ownersService;
    @Autowired
    private PetTypesService petTypesService;
    @Autowired
    private VisitsService visitsService;

    /**
     * 跳转控制器；跳转到宠物搜索页面
     * @return 宠物搜索页面URL
     */
    @RequestMapping("/toPetSearch")
    public String toPetSearch() {
        return "pet/petsearch";
    }

    /**
     * 宠物查询
     * @param petName 宠物名称
     * @param ownName 宠物主人姓名
     * @param model 模型参数
     * @return 查询成功跳转到宠物搜索结果页面；查询失败跳转到宠物搜索页面
     */
    @RequestMapping("/queryPet")
    public String queryPet(String petName, String ownName, Model model) {
        if (!StringUtils.isEmpty(petName)) {
            List<Pets> petsList = petService.queryPetByName(petName);
            if (!petsList.isEmpty()) {
                model.addAttribute("petsList", petsList);
                model.addAttribute("pd", "1");
                model.addAttribute("msg", "宠物信息查询成功！！！");

                return "pet/petresult";
            } else {
                model.addAttribute("msg", "宠物信息查询失败！！！");
                return "pet/petsearch";
            }
        } else if (!StringUtils.isEmpty(ownName)) {
            List<Owners> ownersList = ownersService.queryOwnerLikeName(ownName);

            if (!ownersList.isEmpty()) {
                List<Pets> petsList = new ArrayList<>();
                for (Owners owners : ownersList) {
                    List<Pets> petsList1 = owners.getPetsList();
                    for (Pets pets : petsList1) {
                        pets.setOwners(owners);
                        petsList.add(pets);
                    }
                }
                model.addAttribute("petsList", petsList);
                model.addAttribute("pd", "2");
                model.addAttribute("msg", "宠物信息查询成功！！！");
                return "pet/petresult";
            } else {
                model.addAttribute("msg", "宠物信息查询失败！！！");
                return "pet/petsearch";
            }
        }
        model.addAttribute("msg", "宠物信息查询失败！！！");
        return "pet/petsearch";
    }

    /**
     * 跳转控制器；跳转到宠物搜索结果页面
     * @return 宠物搜索页面URL
     */
    @RequestMapping("/toPetResult")
    public String toPetResult() {
        return "pet/petresult";
    }

    /**
     * 跳转控制器；跳转到兽医和专业绑定页面
     * @return 兽医和专业绑定页面URL
     */
    @RequestMapping("/toDocSearch")
    public String toDocSearch() {
        return "doc/docsearch";
    }

    /**
     * 跳转控制器；跳转到宠物主人新增页面
     * @return 宠物主人新增页面URL
     */
    @RequestMapping("/toOwnersAdd")
    public String toOwnersAdd() {
        return "pet/ownersadd";
    }

    /**
     * 跳转控制器；跳转到宠物新增页面
     * @param model 模型参数
     * @return 查询所有宠物主人，查询所有宠物类别，跳转到宠物新增页面
     */
    @RequestMapping("/toPetAdd")
    public String toPetAdd(Model model) {
        List<Owners> allOwners = ownersService.getAllOwners();
        List<PetTypes> petTypes = petTypesService.getPetTypes();

        model.addAttribute("petBirthDate", DateUtils.DateToStr(new Date()));
        model.addAttribute("petTypes", petTypes);
        model.addAttribute("allOwners", allOwners);

        return "pet/petadd";
    }

    /**
     * 新增宠物
     * @param pets 数据源
     * @param model 模型参数
     * @return 新增成功则跳转到宠物搜索页面；新增失败则跳转到宠物新增页面
     */
    @RequestMapping("/addPet")
    public String addPet(Pets pets, Model model) {
        Owners owners = ownersService.queryOwnerByName(pets.getOwnerName());
        PetTypes petType = petTypesService.getPetTypeByName(pets.getTypeName());
        if (owners != null && petType != null) {
            Date date = new Date();
            pets.setTypeId(petType.getId());
            pets.setOwnerId(owners.getId());
            pets.setBirthDate(date);
            Integer i = petService.insertPet(pets);
            if (i > 0) {
                model.addAttribute("msg", "宠物添加成功！！！");
                return "pet/petsearch";
            } else {
                model.addAttribute("msg", "宠物添加失败！！！");
                return "pet/petadd";
            }
        } else {
            model.addAttribute("msg", "宠物添加失败！！！");
            return "pet/petadd";
        }
    }

    /**
     * 跳转控制器；跳转到宠物更新页面
     * @param petName 被修改的宠物原名称
     * @param model 模型参数
     * @return 查询宠物数据成功则跳转到宠物更新页面；查询失败则跳转到宠物搜索结果页面
     */
    @RequestMapping("/toUpdatePet")
    public String toUpdatePet(String petName, Model model) {
        Pets pet = petService.getPetByName(petName);
        if (pet != null) {
            model.addAttribute("pet", pet);
            model.addAttribute("petBirthDate", DateUtils.DateToStr(pet.getBirthDate()));
            model.addAttribute("msg", "查询成功！！！");
            return "pet/petupdate";
        } else {
            model.addAttribute("msg", "查询失败！！！");
            return "pet/petresult";
        }
    }

    /**
     * 跳转控制器；跳转到宠物更新详细页面
     * @param petName 被修改的宠物原名称
     * @param model 模型参数
     * @return 查询宠物数据成功则跳转到宠物更新详细页面，携带宠物类型数据和宠物主人数据；查询失败则跳转到宠物更新页面
     */
    @RequestMapping("/toUpdatePet01")
    public String toUpdatePet01(String petName, Model model) {
        Pets pet = petService.getPetByName(petName);
        List<PetTypes> petTypes = petTypesService.getPetTypes();
        List<Owners> allOwners = ownersService.getAllOwners();

        if (pet != null) {
            model.addAttribute("pet", pet);
            model.addAttribute("petBirthDate", DateUtils.DateToStr(pet.getBirthDate()));
            model.addAttribute("petTypes", petTypes);
            model.addAttribute("allOwners", allOwners);

            return "pet/petupdate01";
        } else {
            model.addAttribute("msg", "修改失败！！！");
            return "pet/petupdate";
        }
    }

    /**
     * 宠物更新
     * @param pets 数据源
     * @param model 模型参数
     * @return 修改成功则跳转到宠物更新页面；修改失败则跳转到宠物更新详情页面
     */
    @RequestMapping("/updatePet")
    public String updatePet(Pets pets, Model model) {
        PetTypes petTypes1 = petTypesService.getPetTypeByName(pets.getTypeName());
        Owners owners = ownersService.queryOwnerByName(pets.getOwnerName());
        pets.setTypeId(petTypes1.getId());
        pets.setOwnerId(owners.getId());
        /*
        本质上是修改宠物主人ID和宠物类别ID
         */
        Integer i = petService.updatePet(pets);

        if (i > 0) {
            Pets pet = petService.getPetByName(pets.getName());
            model.addAttribute("pet", pet);
            model.addAttribute("petBirthDate", DateUtils.DateToStr(pet.getBirthDate()));
            model.addAttribute("msg", "宠物信息修改成功！！！");
            return "pet/petupdate";
        } else {
            Pets pet = petService.getPetByName(pets.getName());
            List<PetTypes> petTypes = petTypesService.getPetTypes();
            List<Owners> allOwners = ownersService.getAllOwners();
            model.addAttribute("pet", pet);
            model.addAttribute("petBirthDate", DateUtils.DateToStr(pet.getBirthDate()));
            model.addAttribute("petTypes", petTypes);
            model.addAttribute("allOwners", allOwners);
            model.addAttribute("msg", "宠物信息修改失败！！！");
            return "pet/petupdate01";
        }
    }

    /**
     * 跳转控制器；跳转到宠物病历新增页面
     * @param petName 宠物名称
     * @param model 模型参数
     * @return 查询成功则跳转到宠物病历新增页面；查询失败则跳转到宠物更新页面
     */
    @RequestMapping("/toAddVisits")
    public String toAddVisits(String petName, Model model) {
        Pets pet = petService.getPetByName(petName);
        if (pet != null) {
            model.addAttribute("pet", pet);
            model.addAttribute("petBirthDate", DateUtils.DateToStr(pet.getBirthDate()));
            return "pet/addvisit";
        } else {
            model.addAttribute("msg", "查询失败！！！");
            return "pet/petupdate";
        }
    }

    /**
     * 查询宠物病历
     * @param petName 宠物名称
     * @param model 模型参数
     * @return 查询成功则跳转到病历结果页面；查询失败则跳转到宠物更新页面
     */
    @RequestMapping("/lookVisits")
    public String lookVisits(String petName, Model model) {

        Pets pet = petService.getPetByName(petName);

        if (pet != null) {
            List<Visits> visitsList = new ArrayList<>();
            List<Visits> visitsList1 = visitsService.getVisitByPetId(pet.getId());
            for (Visits visits : visitsList1) {
                String s = DateUtils.DateToStr(visits.getVisitDate());
                /*
                根据数据库查询的病例列表，
                将字段`visit_date`的数据（单个宠物病例创建时间）
                存入成员变量`visitDate2`（单个宠物病例创建时间，JSP页面专用成员变量）
                 */
                visits.setVisitDate2(s);
                visitsList.add(visits);
            }
            model.addAttribute("pet", pet);
            model.addAttribute("visitsList", visitsList);
            model.addAttribute("petBirthDate", DateUtils.DateToStr(pet.getBirthDate()));
            return "pet/lookvisit";
        } else {
            model.addAttribute("msg", "查询病历失败！！！");
            return "pet/petupdate";
        }
    }

    /**
     * 跳转控制器；从宠物病历结果页面退出到宠物更新页面
     * @param petName 宠物名称
     * @param model 模型参数
     * @return 宠物更新页面URL
     */
    @RequestMapping("/outlookVisits")
    public String outlookVisits(String petName, Model model) {
        Pets pet = petService.getPetByName(petName);
        model.addAttribute("pet", pet);
        model.addAttribute("petBirthDate", DateUtils.DateToStr(pet.getBirthDate()));
        return "pet/petupdate";
    }
}
